package kr.pe.johnkim.pushchat.mobile.controller;

import kr.pe.johnkim.pushchat.mobile.domain.PushChatMember;
import kr.pe.johnkim.pushchat.mobile.domain.PushChatUser;
import kr.pe.johnkim.pushchat.mobile.service.PushChatMemberService;
import kr.pe.johnkim.pushchat.mobile.service.PushChatUserService;
import kr.pe.johnkim.pushchat.mobile.util.PushChatCommon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 6. 12.
 */
@Controller
@RequestMapping(value = "/pushChatUserController")
public class PushChatUserController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(PushChatUserController.class);

    @Autowired
    private PushChatUserService pushChatUserService;

    @Autowired
    private PushChatMemberService pushChatMemberService;

    @Autowired
    private PushChatCommon pushChatCommon;

    /**
     * 채팅방에 사용자를 추가 한다.
     *
     * @param pushChatUser
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/registerUser", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatUser registerUser(@RequestBody PushChatUser pushChatUser) throws Exception {
        logger.info("registerUser");

        PushChatUser result = new PushChatUser();

        try {
            this.pushChatUserService.registerUser(pushChatUser);
            result.setResult("SUCCESS");
            logger.info("success to register user.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to register user.");
        }

        /*
        사용자를 추가하면 추가된 사용자에게 Push Notification 을 발송한다.
         */
        if(("SUCCESS").equals(result.getResult())) {
            PushChatMember parameter = new PushChatMember();

            parameter.setMemberId(pushChatUser.getMemberId());
            parameter.setActivateYn(pushChatUser.getActivateYn());

            PushChatMember memberInformation = this.pushChatMemberService.selectMemberInformation(parameter);

            if(("AS").equals(memberInformation.getDeviceType())) {
                this.pushChatCommon.sendASMessage(memberInformation.getRegistrationId(), "NEW_CHAT");
            } else if(("IS").equals(memberInformation.getDeviceType())) {
                this.pushChatCommon.testSendIPMessage(memberInformation.getRegistrationId(), "NEW_CHAT", pushChatUser.getPushChatChatIdChat(), pushChatUser.getPushChatChatOwner());
            }
        }

        return result;
    }

    /**
     * 채팅방의 사용자를 비활성화 한다.
     *
     * @param pushChatUser
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/updateDeactivateUser", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatUser updateDeactivateUser(@RequestBody PushChatUser pushChatUser) throws Exception {
        logger.info("updateDeactivateUser");

        PushChatUser result = new PushChatUser();

        try {
            this.pushChatUserService.updateDeactivateUser(pushChatUser);
            result.setResult("SUCCESS");
            logger.info("success to update deactivate user.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update deactivate user.");
        }

        return result;
    }

    /**
     * 채팅방 사용자 목록을 가져 온다.
     *
     * @param pushChatUser
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectUserList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<PushChatUser> selectUserList(@RequestBody PushChatUser pushChatUser) throws Exception {
        logger.info("selectUserList");
        return this.pushChatUserService.selectUserList(pushChatUser);
    }

    /**
     * 채팅방 사용자 정보를 가져 온다.
     *
     * @param pushChatUser
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectUserInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatUser selectUserInformation(@RequestBody PushChatUser pushChatUser) throws Exception {
        logger.info("selectUserInformation");

        return this.pushChatUserService.selectUserInformation(pushChatUser);
    }
}
